package com.schoolET.controller.wechat;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.dto.WechatAndRegionDTO;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IGoodsTypeService;
import com.schoolET.service.wechat.GoodsService;
import com.schoolET.service.wechat.IWechatAndRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: le
 * @className: WechatLoginController
 * @createTime: 2024-06-06  10:59
 * @description: 用户区域控制器
 */
@Api(tags = "用户区域")
@RestController
@RequestMapping("/schoolET/wx")
public class WechatRegionController {

    @Resource
    private IWechatAndRegionService wechatAndRegionService;
    @Resource
    private IGoodsTypeService goodsTypeService;
    @Resource
    private GoodsService goodsService;

    @SaCheckLogin
    @ApiOperationSupport(author = "le")
    @ApiOperation(value ="加入区域")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "code", value = "区域code", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/invitation/{code}")
    public Result<Object> joinRegion(@PathVariable Integer code) {
        String openId = StpUtil.getLoginIdAsString();
        boolean result = wechatAndRegionService.addWechatAndRegion(code, openId);
        if(!result){
            return Result.error("加入区域失败");
        }
        return Result.success().msg("加入区域成功");
    }
    @ApiOperationSupport(author = "le")
    @ApiOperation(value ="区域地址列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/regionList/{id}")
    public Result regionList(@PathVariable Integer id) {
        List<WechatAndRegionDTO> wechatAndRegions = wechatAndRegionService.regionList(id);
        return Result.success(wechatAndRegions).msg("查询区域地址列表成功");
    }
    @ApiOperationSupport(author = "le")
    @ApiOperation(value ="退出区域")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户-区域表id", dataType = "Integer", paramType = "query")
    })
    @SaCheckLogin
    @PostMapping("/regionRemove/{id}")
    public Result regionRemove(@PathVariable Integer id) {
        boolean result = wechatAndRegionService.removeById(id);
        if (!result) return Result.error("退出失败");
        return Result.success().msg("退出成功");
    }
    @ApiOperationSupport(author = "le")
    @ApiOperation(value ="切换区域列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",required = true, value = "区域Id")
    })
    @SaCheckLogin
    @PostMapping("/changeRegion/{id}")
    public Result changeRegion(@PathVariable Integer id) {
        String openId = StpUtil.getLoginIdAsString();
        boolean result = wechatAndRegionService.updateDefaultLocation(id, openId);
        if (!result) return Result.error("切换区域失败,请稍后重试");
        return Result.success().msg("切换区域列表成功");
    }
}
